import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import mutations from './mutations'
import getters from './getters'
const syncStorageCart = store => {
  store.subscribe((mutation, state) => {
    // 每次mutation之后调用
    // mutation格式为{ type，payload }
    window.localStorage.setItem('cart', JSON.stringify(state.cart))
  })
}
const syncStorageUser = store => {
  store.subscribe((mutation, state) => {
    // 每次mutation之后调用
    // mutation格式为{ type，payload }
    window.localStorage.setItem('user', JSON.stringify(state.user))
  })
}

Vue.use(Vuex)

export default new Vuex.Store({
  strict: true,
  state,
  getters,
  plugins: [syncStorageCart, syncStorageUser],
  mutations
})
